import TableLyout from '@/components/commonPublic/tableLyout.vue'
import tree from "@/components/materialQualityPage/constructionSiteLaboratoryManage/tree"; //左侧机构树
import publicForm from "@/components/public/el-form.vue"; //弹框form

import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getUserCompanyTreeList, //树状图
} from "@/api/request.js";
import {
    getLaboratoryInfo, //详情
    saveLaboratoryInfo, //保存
} from "@/api/BackStage/materialQualityPage/request.js";

export default {
    name: "laboratoryInformation",
    data() {
        return {
            name: "laboratoryInformation",
            queryCriteriaData: {}, //分页
            navigatepageNums: null,
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            expandLoading: false,
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            // checked: true, //查看下级按钮
            showName: "未选择公司",
            checkData: [], //树状图默认选中id
            isLock: false,
            loading: false,
            btnObj: [{
                fEncode: "yj_submit",
                fFullname: "保存",
                fOrder: 3
            }], //按钮
            formObj: formJs.formObj,
            formData: {
                companyId: "", //标段id
                companyName: "", //标段名称
                labName: "", //试验室名称
                authorizedAgency: "", //授权机构
                registration: "", //登记证书
                setdates: this.common.getDay1(0), //设立日期
                certificateNumber: "", //证书编号
                monitoringEngineer: "", //试验室检测工程师
                testInspector: "", //试验检测员
                houseArea: "", //试验检测用房总面积
                certigier: "", //授权负责人
                certificateNumber2: "", //证书编号2
                professionalTitle: "", //职称
                telephone1: "", //联系电话
                labAddress: "", //工地试验室地址
                postcode: "", //邮编
                telephone2: "", //联系电话2
                additionalInfo: "", //附加资料
                scope: "",//授权业务范围
                recordfiles: []
            },
            getList: {},
            fileList: [],
            userInfo: null,
            fNature: null

        };
    },
    components: {
        TableLyout,
        publicForm,

        tree,
        elBreadcrumb,

    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        // console.log(this.userInfo.companyNature);
        this.fNature = this.userInfo.companyNature;
        // this.userInfo.fNature=JSON.parse(sessionStorage.getItem("yjSmart-userInfo")).fNature;
        if (JSON.parse(sessionStorage.getItem("yjSmart-constructionSiteLaboratoryManage_treeCheck"))) {
            var data = JSON.parse(sessionStorage.getItem("yjSmart-constructionSiteLaboratoryManage_treeCheck"));
            this.checkData = [data.fCompanyid];
            this.fNature = data.fNature;
        }
        // this.yj_searchForm(this.getList);
        this.getTree();
        // this.btnRole(this.name)
    },
    methods: {


        closeDialog() {
            this.isShow = false;
        },
        // 左侧树结构
        getTree() {
            console.log(1111);
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyId;
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }
                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-constructionSiteLaboratoryManage_treeCheck")
                            ).fShortname;
                            companyId = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }
                        this.getList.companyId = companyId;
                        this.getList.companyName = this.showName;

                        this.formData.companyId = companyId;
                        this.formData.companyName = this.showName;

                        this.getDetail({
                            companyId: companyId
                        }); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },
        // 新增/编辑
        yj_edit(num) {

            this.statusNum = num
            if (num == 3) { //查看
                this.saveForm(this.formData)
            }
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            console.log(data);
            this.showName = data.fShortname; //顶部显示机构名
            this.fNature = data.fNature;

            if (data.fNature == '施工单位' || data.fNature == '中心试验室') {
                // this.getList.fCompanyid = data.fCompanyid;
                // this.getList.page = 1;
                // this.getList.pageSize = 20;
                this.formData.companyId = data.fCompanyid;
                this.formData.companyName = this.showName;
                this.getList.companyId = data.fCompanyid;
                this.getList.companyName = this.showName;
                this.getDetail({
                    companyId: data.fCompanyid
                });
            }


        },
        handleRow() { },



        editText() {
            this.isShow = true;
        },

        // 详情
        getDetail(obj) {
            this.loading = true;
            getLaboratoryInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpMsLaboratory;
                    this.formData = data;
                    if (data.fId == null) {
                        this.formData.setdates = this.common.getDay1(0)//设立日期
                    }
                    this.fileList = res.data.fFilelist;
                    this.formData.recordfiles = []
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }

            });
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 保存
        saveForm(obj) {
            this.formData.companyId = this.getList.companyId;
            this.formData.companyName = this.getList.companyName;
            this.loading = true;
            this.isLock = true
            saveLaboratoryInfo(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;
                        this.visible = false;

                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};